package cn.idietrade.idietradealipaymodule8012.controller;

import cn.idietrade.idietradealipaymodule8012.service.PayService;
import cn.idietrade.idietradealipaymodule8012.util.AlipayConfig;
import cn.idietrade.idietradealipaymodule8012.util.RabbitMQUtil;
import com.alipay.api.AlipayApiException;
import com.alipay.api.internal.util.AlipaySignature;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.Map;

@RestController
public class PayController {
    @Resource
    private PayService payService;

    @Resource
    private RabbitMQUtil rabbitMQUtil;

    @RequestMapping("/pay")
    public void buy(HttpServletResponse resp,String token ,Integer totalAmount,String subject){
        try {
           payService.alipay(resp,totalAmount,subject);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @RequestMapping("/send")
    public void send(){
        rabbitMQUtil.sendMessage("aa");
    }



    @RequestMapping("/returnUrl")
    public String returnUrl(HttpServletRequest request){
        try {
        //支付宝支付成功之后返回
        Map<String, String[]> paramMap = request.getParameterMap();
        Map<String, String> param = new HashMap<String, String>();
        for(String key:paramMap.keySet()){
            Object [] obj = paramMap.get(key);
            String value = "";
            for(Object object:obj){
                value = object +",";
                value = value.substring(0,value.length()-1);
            }
            param.put(key,value);
        }

        //对支付宝的返回结果进行验签，判断是否成功

            boolean flag = AlipaySignature.rsaCheckV1(param, AlipayConfig.ALIPAY_PUBLIC_KEY, AlipayConfig.CHARSET, AlipayConfig.sign_type);
            if (flag){
                String outTradeNo = param.get("out_trade_no");
                String tradeNo = param.get("trade_no");
                Double price= Double.valueOf(param.get("total_amount"));
                return "同步支付成功,订单号为:"+outTradeNo+"交易号："+tradeNo+"金额："+price;
            }else {
                return "同步支付失败";
            }
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        return "同步支付失败";
    }

    @RequestMapping("/notifyUrl")
    public String notifyUrl(HttpServletRequest request){
        Map<String, String[]> paramMap = request.getParameterMap();
        return "异步支付成功";
    }


    /**
     * 支付宝退款
     */
    @PostMapping("/refund")
    public String alipayRefund(String outTradeNo,String tradeNo,
                                        String refundAmount,String subject) {
        boolean result = false;
        try {
            result = payService.alipayRefund(outTradeNo, tradeNo, refundAmount, subject);
        } catch (AlipayApiException e) {
            e.printStackTrace();
        }
        if (result){
            return "退款成功";
        }else {
            return "退款失败";
        }
    }

    // 退费查询
    @PostMapping("/RefundSelect")
    public String alipayRefundCallback(String outRequestNo,String tradeNo) {
        boolean result = false;
        result = payService.alipayRefundSelect(outRequestNo, tradeNo);
        if (result){
            return "退款成功";
        }else {
            return "退款失败";
        }
    }


}
